package com.douyin.controller;

import com.douyin.entity.video.Video;
import com.douyin.holder.UserHolder;
import com.douyin.limit.Limit;
import com.douyin.service.FileService;
import com.douyin.service.QiNiuFileService;
import com.douyin.service.VideoService;
import com.douyin.utils.R;
import com.douyin.vo.BasePage;
import org.eclipse.angus.mail.imap.AppendUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;


@RestController
@RequestMapping("/douyin/video")
public class VideoController {
    @Autowired
    private VideoService videoService;
    @Autowired
    private QiNiuFileService qiNiuFileService;


    //获取文件上传token
    @GetMapping("/token")
    public R getToken(){
        return R.ok().data(qiNiuFileService.getToken());
    }
    @PostMapping
    @Limit(limit = 5,time = 360L,msg = "发布视频一小时内不可超过5次")
    public R publishVideo(@RequestBody @Validated Video video){
        videoService.pushVideo(video, UserHolder.get());
        return R.ok().message("发布成功，请等待审核");
    }

    @DeleteMapping("/{id}")
    public R deleteVideo(@PathVariable Long id){
        videoService.deleteVideo(id,UserHolder.get());
        return R.ok().message("删除成功");
    }

    @GetMapping
    public R listVideo(BasePage basePage){
        return R.ok().data(videoService.listByUserIdVideo(basePage,UserHolder.get()));
    }
    @PostMapping("/favorites/{fId}/{vId}")
    public R favoritesVideo(@PathVariable Long fId,@PathVariable Long vId){
        String message = videoService.favorites(fId,vId,UserHolder.get())?"收藏成功":"取消收藏成功";
        return R.ok().message(message);
    }

    @GetMapping("/favorites/{favoritesId}")
    public R getFavoritesVideo(@PathVariable Long favoritesId){
        return R.ok().data(videoService.listVideoByFavoritesId(favoritesId,UserHolder.get()));
    }

    @PostMapping("/star/{videoId}")
    public R starVideo(@PathVariable Long videoId){
        String msg = "已点赞";
        if (!videoService.starVideo(videoId, UserHolder.get())) {
            msg = "取消点赞";
        }
        return R.ok().message(msg);
    }

    @PostMapping("/history/{id}")
    public R getHistoryVideo(@PathVariable Long id){
        videoService.historyVideo(id,UserHolder.get());
        return R.ok();
    }

    @GetMapping("/history")
    public R getHistory(BasePage basePage){
        return R.ok().data(videoService.getHistory(basePage,UserHolder.get()));
    }

    @GetMapping("/audit/queue/state")
    public R getAuditQueueState(){
        return R.ok().data(videoService.getAuditQueueState());
    }

    @GetMapping("/follow/feed")
    public R getFollowFeed(@RequestParam(required = false)Long lastTime){
        return R.ok().data(videoService.followFeed(UserHolder.get(),lastTime));
    }
    @PostMapping("/init/follow/feed")
    public R initFollowFeed(){
        videoService.updateFollowFeed(UserHolder.get());
        return R.ok();
    }
}

